#!/bin/bash
NEWFLOPPY="false"

if test ! -e "./fluidium_floppy.img"; then NEWFLOPPY="true"; fi

cd ./mnt && sudo rm -rf * && cd ..

echo Floppy Disk Installation...
if test $NEWFLOPPY = "true"; then dd if=/dev/zero of=./fluidium_floppy.img bs=2880 count=512; fi
sudo losetup /dev/loop0 ./fluidium_floppy.img
if test $NEWFLOPPY = "true"; then sudo mkfs -t vfat /dev/loop0; fi
sudo mount -t vfat /dev/loop0 ./mnt
cd ./mnt
PACKAGES=$(find ../../kernel ../../programs -mindepth 1 -maxdepth 3 -name "*.pkg.tgz")
for i in $PACKAGES; do
	sudo tar xzf $i
done
cd ..
sudo grub --device-map=./device_floppy.map <<EOF
install (fd0)/packages/grub/stage1 (fd0) (fd0)/packages/grub/stage2 (fd0)/config/grub/menu_floppy.cfg
quit
EOF
sudo umount /dev/loop0
sudo losetup -d /dev/loop0

echo Hard Disk Installation...
if test ! -e "./fluidium_disk.img"; then cp ./fluidium_disk_template.img ./fluidium_disk.img; fi
sudo losetup -o1999872 /dev/loop0 ./fluidium_disk.img
sudo mount -t vfat /dev/loop0 ./mnt
cd ./mnt
PACKAGES=$(find ../../kernel ../../programs -mindepth 1 -maxdepth 3 -name "*.pkg.tgz")
for i in $PACKAGES; do
	sudo tar xzf $i
done
cd ..
sudo umount /dev/loop0
sudo losetup -d /dev/loop0

